
#ifndef _DEV_WASHER_H_
#define _DEV_WASHER_H_

#include "sys_port.h"
#include "dev_off.h"
#include "dev_off_line.h"
#include "root.h"


//#define WASH_LOOP_TEST

#define PROACTIVELY_UPDATE_UI //按下按键先更新状态

#define TIMER_FREQ 250

#define WH_SEC_CNT (1000/TIMER_FREQ) //定时器每秒计数

#define WH_CLICK_LOCK_TIME (3 * WH_SEC_CNT) // 3 second 

#define WASHER_TO_SPEC_WAIT_TIME (10 * WH_SEC_CNT)

#ifdef WASH_LOOP_TEST
#define MIN_CNT 1
#else
#define MIN_CNT 60
#endif

#define TIME_OUT_CHECK 	//超时主动查询



//#define USE_ERR_PAGE

typedef struct 
{
    char  devid[20];
    uint32_t  ctl_sta;
	uint32_t version;
	uint8_t mode[24];
}washer_http_req_t;


typedef enum washer_work_stat
{
	WASHER_STAT_POWER_OFF = 0,   //已关机
    WASHER_STAT_STANDBY, 	//待机
	WASHER_STAT_ORDER,      //预约中
	WASHER_STAT_WORK,   	//洗涤中
    WASHER_STAT_DRYING,     //烘干中
    WASHER_STAT_KEEPING,     //保管中,
	WASHER_STAT_PAUSE,   	//暂停中
	//WASHER_STAT_DRY_PAUSE,
	WASHER_STAT_ERROR,
	//WASHER_STAT_DOOR_OPEN,
	WASHER_STAT_OFFLINE,
}en_washer_work_stat;


//协议1.0：
//wash_stage：1/预洗，2/主洗，3/漂洗，4/干燥，5/结束
typedef enum washing_stat
{
    WASHING_STAT_PRE = 1, 	//预洗
    WASHING_STAT_MAIN,   	//主洗
	WASHING_STAT_RINSE,   	//漂洗
    WASHING_STAT_DRY,       //干燥
	WASHING_STAT_DONE   	//结束
}en_washing_stat;


#if 0
/////////////////////////////
工作状态:
协议1.0：
work_status为error，故障中
air_left_hour大于0且air_status不等于0，保管中
dryswitch为2，烘干中
operator为pause，已暂停
work_status为power_off，已关机
work_status为work，洗涤中
work_status为order，预约中
其他为待机

协议2.0
work_status为
power_off，已关机
power_on，待机中
order，预约中
work，洗涤中
error，故障中
order_pause，预约暂停
work_pause，洗涤暂停
dry，烘干中
keep，保管中
dry_pause，烘干暂停
keep_pause，保管暂停
ota，ota中
drain，排水中
////////////////////////////////
开关门状态
协议1.0：
doorswitch：0/开门，1/关门

协议2.0：
base_door_on：1/开门，0/关门

#endif
#pragma pack(push, 1) 

typedef struct
{
	uint8_t version;
	uint8_t status;	//0:work,1:off,2 offline
	uint8_t work_state;
	uint8_t door_state; //开关门状态  //1开门，0关门
	uint8_t wash_stage; //洗涤状态:预洗/主洗/漂洗/干燥
	uint8_t sub_state;
	int run_left_time; //运行剩余时间
	int order_left_time; //预约剩余时间
	int keep_left_time; //保管剩余时间
	int dry_left_time;  //烘干剩余时间
	uint8_t lock; 		//V1.0 童锁 打开时（lock为true），则不能启动，暂停，取消
	uint8_t err_code;
	uint8_t mode[24];
}washer_sta_dat_t;

#pragma pack(pop)

typedef enum wh_ctl
{
    WASHING_CTL_POWER_ON, 		//待机
    WASHING_CTL_POWER_OFF,   	//关机
	WASHING_CTL_START,   		//启动
    WASHING_CTL_PAUSE,  		//暂停
	WASHING_CTL_RESUME,  		//继续
	WASHING_CTL_CANCLE,   		//取消预约
	WASHING_CTL_ORDER_CANCLE,
	WASHING_CTL_DRY_CANCLE,
	WASHING_CTL_KEEP_CANCLE,	//取消保管
}en_wh_ctl;

typedef struct
{
	uint8_t ctl_state;
	
}washer_ctl_data_t;

typedef struct
{
	char      devname[64];
    char      devid[20];
    char      devtype[12];
    char      devsubtype[12];
} washer_base_data_t;

typedef struct sta_ctl
{
	uint8_t got_data;
	uint16_t cur_tick;
	uint8_t update_flag;
	uint8_t spe_flag; //1 washing done,2,cancle
}st_sta_ctl;

typedef struct 
{
	washer_base_data_t base;
	washer_sta_dat_t state;
	washer_ctl_data_t ctl;
	st_sta_ctl 		stactl;

}data_washer_t;


////////////////////////////////////////////////////////////////////////
typedef enum wh_ui_state
{
	wh_ui_stat_power_off=0,
	wh_ui_stat_standby, //待机
	wh_ui_stat_order,//预约中
	wh_ui_stat_work, //洗涤中
	wh_ui_stat_dry,//烘干中
	wh_ui_stat_keep, //保管
	wh_ui_stat_spec_work, //特殊工作状态-洗涤中
	wh_ui_stat_dialog,	//dialog
	wh_ui_stat_door_open,
	wh_ui_stat_pause,
	wh_ui_stat_error,
	wh_ui_stat_off_line,
	wh_ui_stat_invalid,
}en_wh_ui_state;

typedef struct ui_wh_para
{
	uint8_t dev_idx;
	uint8_t create_status;
	uint8_t ui_stat;
	uint8_t ui_last_stat;
	bool in_spec_mod; //is ui in spec mode now
	uint8_t timer_step;
	uint8_t cur_state;
	int state_tick;
	bool washing;
	char bar_value;
	char flycnt;
	int ct_sec;
	int init_work_time;
	uint8_t tick;
	uint8_t locked;
	uint8_t lock_tick;
	uint8_t work_state;
}st_ui_wh_para;


typedef struct 
{
	st_ui_wh_para ui_para;
    uint8_t last_stat;

    lv_timer_t * timer;

	lv_obj_t* bg;
    lv_obj_t* bg_img;

    lv_obj_t *label_info;
	lv_obj_t *label_info2;
	lv_obj_t *l_btn;
	lv_obj_t *l_btn_img;
	lv_obj_t *r_btn;
	lv_obj_t *r_btn_img;

	lv_obj_t *cont_spec_mode;
	lv_obj_t *lbl_info;
	lv_obj_t *lbl_unit1;
	lv_obj_t *lbl_unit2;
	lv_obj_t *lbl_data1;
	lv_obj_t *lbl_data2;

	lv_obj_t *cont_dialog;
	lv_obj_t *btn_gotit;
	lv_obj_t *btn_gotit_label;
	lv_obj_t *img_ok;
	lv_obj_t *label_note;
	lv_obj_t *label_sec;

	lv_obj_t *bar_1;

	ui_dev_off_t      dev_off;
    ui_dev_off_line_t dev_off_line;
	#ifdef USE_ERR_PAGE
	ui_dev_err_e2_hide(&dev->ui.dev_e2);
	#endif
	
} ui_washer_t;


typedef struct 
{
    data_washer_t data;
    ui_washer_t  ui;
}dev_washer_t;

dev_info_t* dev_washer_reg(data_washer_t* dev);
page_info_t* dev_washer_page_reg(dev_washer_t* washer);


//extern dev_washer_t* pwh;
//dev_info_t* dev_washer_reg(data_washer_t* dev);
//int dev_washer_unreg(dev_info_t* devinfo);
void washer_stat_proc(dev_washer_t *dev);
void washer_pwr_on_click(lv_event_t* event,dev_washer_t* dev);
void fsm_washer_test(dev_washer_t* dev);
#ifdef WASH_LOOP_TEST
void rst_test_loop();
void clr_wh_tim_cnt();
#endif

#endif
